package com.ts.quartz.control;

import com.ts.comm.SysDate;
import com.ts.comm.SysNumber;
import com.ts.comm.SysResult;
import com.ts.login.bean.UserInfo;
import com.ts.quartz.bean.ScheduleInfo;
import com.ts.quartz.dao.ScheduleDao;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;

/**
 * Created by yingzi on 2017-6-22.
 */
@Controller
public class ScheduleController {

    @Resource
    private ScheduleDao scheduleDao;
    /**
     * 查询任务信息
     * */
    @RequestMapping(value = "/task/taskList",method = RequestMethod.POST)
    @ResponseBody
    public SysResult queryTasks(HttpServletRequest req){
        SysResult sr = scheduleDao.queryJobs();
        return sr;
    }

    /**
     * 查询单条任务信息
     * */
    @RequestMapping(value = "/task/getTask",method = RequestMethod.POST)
    @ResponseBody
    public SysResult getTask(HttpServletRequest request, HttpServletResponse response){
        SysResult sr = new SysResult();
        try {
            request.setCharacterEncoding("utf-8");
            response.setCharacterEncoding("utf-8");
            int iFid= SysNumber.toInt(request.getParameter("FID"));
            sr = scheduleDao.getJob(iFid);
        } catch (Exception e) {
            sr.onSetErrorMsg(e);
        }
        return sr;
    }

    /**
     * 添加任务
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/task/addTask",method = RequestMethod.POST)
    @ResponseBody
    public SysResult addTask(HttpServletRequest request, HttpServletResponse response){
        SysResult sysResult = new SysResult();
        try {
            request.setCharacterEncoding("utf-8");
            response.setCharacterEncoding("utf-8");
            UserInfo u= (UserInfo) request.getSession().getAttribute("user");
            String sBeanName=request.getParameter("beanName");
            String sMethodName=request.getParameter("methodName");
            String sParams=request.getParameter("params");
            String sCronExp=request.getParameter("cronExp");
            String sRemark=request.getParameter("remark");
            ScheduleInfo scheduleInfo=new ScheduleInfo();
            scheduleInfo.setBeanName(sBeanName);
            scheduleInfo.setMethodName(sMethodName);
            scheduleInfo.setCronExpr(sCronExp);
            scheduleInfo.setParams(sParams);
            scheduleInfo.setRemark(sRemark);
            scheduleInfo.setStatus(0);//正常
            scheduleInfo.setCreateUser(u.getUserCode());
            scheduleInfo.setCreateTime(SysDate.getNowDateTime());
            sysResult = scheduleDao.addJob(scheduleInfo);
        } catch (Exception e) {
            sysResult.onSetErrorMsg(e);
        }
        return sysResult;
    }

    /**
     * 删除任务
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/task/deleteTask",method = RequestMethod.POST)
    @ResponseBody
    public SysResult deleteTask(HttpServletRequest request, HttpServletResponse response){
        SysResult sysResult = new SysResult();
        try {
            request.setCharacterEncoding("utf-8");
            response.setCharacterEncoding("utf-8");
            int iFID=SysNumber.toInt(request.getParameter("FID"));
            sysResult=scheduleDao.deleteJob(iFID);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return sysResult;
    }

    /**
     * 修改任务信息
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/task/updateTask",method = RequestMethod.POST)
    @ResponseBody
    public SysResult editTask(HttpServletRequest request, HttpServletResponse response){
        SysResult sysResult = new SysResult();
        try {
            request.setCharacterEncoding("utf-8");
            response.setCharacterEncoding("utf-8");
            int iFID=SysNumber.toInt(request.getParameter("id"));
            String sBeanName=request.getParameter("beanName");
            String sMethodName=request.getParameter("methodName");
            String sParams=request.getParameter("params");
            String sCronExp=request.getParameter("cronExp");
            String sRemark=request.getParameter("remark");
            ScheduleInfo scheduleInfo=new ScheduleInfo();
            scheduleInfo.setBeanName(sBeanName);
            scheduleInfo.setMethodName(sMethodName);
            scheduleInfo.setCronExpr(sCronExp);
            scheduleInfo.setParams(sParams);
            scheduleInfo.setRemark(sRemark);
            sysResult = scheduleDao.updateJob(scheduleInfo,iFID);
        } catch (Exception e) {
            sysResult.onSetErrorMsg(e);
        }
        return sysResult;
    }
}
